package org.ajc.auth.controller;

import io.swagger.annotations.ApiOperation;
import org.ajc.auth.service.IMenuService;
import org.ajc.auth.domain.Menu;
import org.ajc.auth.query.MenuQuery;
import org.ajc.base.annotation.AjcPermission;
import org.ajc.base.util.AjaxResult;
import org.ajc.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;
    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult save(@RequestBody Menu menu){
        try {
            if( menu.getId()!=null)
                menuService.update(menu);
            else
                menuService.insert(menu);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            menuService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除多个信息
    */
    @DeleteMapping
    public AjaxResult batchDelete(List<Serializable> ids){
        try {
                menuService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id")Long id)
    {
        try {
            Menu menu = menuService.selectById(id);
            return AjaxResult.success(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult selectAll(){

        try {
            List< Menu> list = menuService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }
    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult queryPage(@RequestBody MenuQuery query)
    {
        try {
            PageInfo<Menu> pageInfo = menuService.queryPage(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }

    /**
     * 根据用户的id 查询对应的菜单
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    public AjaxResult queryMenuTree(@PathVariable("id") Long id){
        try {
            List<Menu> tree = menuService.queryMenuTree(id);
            return AjaxResult.success(tree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询菜单树失败！"+e.getMessage());
        }
    }
    /**查询上级菜单*/
    @GetMapping("/menuTree")
    @ApiOperation("查询上级菜单")
    @AjcPermission(name = "查询上级菜单",desc = "查询上级菜单")
    public AjaxResult deptTree(){
        try {
            List<Menu> menuTree = menuService.menuTree();
            if (Objects.nonNull(menuTree)){
                return AjaxResult.success(menuTree);
            }else{
                return AjaxResult.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
